package com.myorg.domain;

import java.io.IOException;

import org.apache.hadoop.fs.FSDataOutputStream;

import com.myorg.search.condition.SearchCondition;
import com.myorg.util.CompressionHelper;

public class Email {
	private String id;
	private String summary;
	private String content;
	private int score;
	private boolean partial;

	// More ...

	public Email() {
		super();
	}

	public String getId() {
		return id;
	}

	public Email setId(String id) {
		this.id = id;
		return this;
	}

	public String getSummary() {
		return summary;
	}

	public Email setSummary(String summary) {
		this.summary = summary;
		return this;
	}

	public int getScore() {
		return score;
	}

	public Email setScore(int score) {
		this.score = score;
		return this;
	}

	public String getContent() {
		return content;
	}

	public Email setContent(String content) {
		this.content = content;
		return this;
	}

	public boolean isPartial() {
		return partial;
	}

	public Email setPartial(boolean partial) {
		this.partial = partial;
		return this;
	}

	public void persist(FSDataOutputStream targetOutputStream, boolean compress) {
		try {
			EmailDelimiter del = new EmailDelimiter(getId());
			if (compress) {
				CompressionHelper.compressAndSave(getContent(), del.toString(),
						targetOutputStream);
			} else {
				targetOutputStream.writeBytes(getContent() + "\n");
				targetOutputStream.writeBytes(del.toString() + "\n");
			}
		} catch (IOException ee) {
			ee.printStackTrace();
			throw new RuntimeException(ee);
		}
	}

	public int getSize() {
		return content == null ? 0 : content.length();
	}

	public String matchAndSummarize(SearchCondition searchCondition) {
		return searchCondition.matchAndSummarize(content);
	}
}
